package com.example.qsindusty.jmm_spline;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bm.library.PhotoView;
import com.example.qsindusty.jmm_spline.Config.Config;
import com.example.qsindusty.jmm_spline.weight.PhotoViewPager;
import com.yixin.ystartlibrary.base.BaseAppCompatActivity;
import com.yixin.ystartlibrary.utils.ImageUtil;
import com.yixin.ystartlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PicActivity extends BaseAppCompatActivity {

    @BindView(R.id.viewPager)
    PhotoViewPager mViewPager;
        MyImageAdapter adapter ;
        int currentPosition =0;
    List<String> pathlist ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic);
        ButterKnife.bind(this);
        setEnabledNavigation(true);

        StatusBarUtil.immersive(this.getWindow());
        StatusBarUtil.setPaddingSmart(this, mToolBar);
    }

    @Override
    protected void init() {

      pathlist= getIntent().getStringArrayListExtra("data");
        currentPosition = getIntent().getIntExtra("position",0);
        setCenterTitle((currentPosition+1)+"/"+pathlist.size());
        adapter = new MyImageAdapter(pathlist, this);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(currentPosition, false);
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentPosition = position;
            }
        });
    }

}


class MyImageAdapter extends PagerAdapter {
   private List<String> imageUrls;
   private AppCompatActivity activity;

   public MyImageAdapter(List<String> imageUrls, AppCompatActivity activity) {
       this.imageUrls = imageUrls;
       this.activity = activity;
   }

   @Override
   public Object instantiateItem(ViewGroup container, int position) {
       String url = imageUrls.get(position);
       url= Config.URL_JMMURL+url;
       final PhotoView photoView = new PhotoView(activity);
        photoView.setScaleType(ImageView.ScaleType.FIT_XY);
       photoView.enable();
       ImageUtil.displayPic(activity, url, photoView);

       container.addView(photoView);
       return photoView;
   }

   @Override
   public int getCount() {
       return imageUrls != null ? imageUrls.size() : 0;
   }

   @Override
   public boolean isViewFromObject(View view, Object object) {
       return view == object;
   }

   @Override
   public void destroyItem(ViewGroup container, int position, Object object) {
       container.removeView((View) object);
   }

   @Override
   public int getItemPosition(Object object) {
       return POSITION_NONE;
   }
}

